<template>
  <el-main>
    <sys-dialog
      :title="sysDialog.title"
      :width="sysDialog.width"
      :height="sysDialog.height"
      :visible="sysDialog.visible"
      @onClose="sysDialogClose"
      @onConfirm="sysDialogConfirm"
    >
      <div slot="content">
        <el-form
          ref="entityForm"
          :model="entityModel"
          :inline="true"
          label-width="90px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="日志标题：">{{ entityModel.title }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作用户：">{{ entityModel.createBy }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请求映射：">{{ entityModel.mapping }}</el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请求方式：">{{ entityModel.mode }}</el-form-item>
            </el-col>
            <el-col v-if="entityModel.method !== null" :span="24">
              <el-form-item label="方法名称：">{{ entityModel.method }}</el-form-item>
            </el-col>
            <el-col v-if="entityModel.params !== null" :span="24">
              <el-form-item label="请求参数："><div class="show-box">{{ entityModel.params }}</div></el-form-item>
            </el-col>
            <el-col v-if="entityModel.result !== null" :span="24">
              <el-form-item label="请求结果："><div class="show-box">{{ entityModel.result }}</div></el-form-item>
            </el-col>
            <el-col v-if="entityModel.exception !== null" :span="24">
              <el-form-item label="异常信息："><div class="show-box">{{ entityModel.exception }}</div></el-form-item>
            </el-col>
            <el-col v-if="entityModel.startTime !== null" :span="12">
              <el-form-item label="开始时间：">{{ entityModel.startTime }}</el-form-item>
            </el-col>
            <el-col v-if="entityModel.endTime !== null" :span="12">
              <el-form-item label="结束时间:">{{ entityModel.endTime }}</el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
    <el-form
      ref="searchForm"
      :model="searchModel"
      :rules="searchRules"
      :inline="true"
      label-width="80px"
    >
      <el-form-item v-if="checkPermission(['sys:log:search'])" prop="title">
        <el-input v-model="searchModel.title" placeholder="请输入日志标题" />
      </el-form-item>
      <el-form-item>
        <el-button v-if="checkPermission(['sys:log:search'])" icon="el-icon-search" @click="searchHandler()">查询</el-button>
        <el-button v-if="checkPermission(['sys:log:search'])" icon="el-icon-delete" @click="resethHandler()">重置</el-button>
        <el-button v-if="checkPermission(['sys:log:clear'])" type="primary" icon="el-icon-plus" @click="clearHandler()">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      style="width: 100%;margin-bottom: 18px;"
      row-key="id"
      border
      stripe
      highlight-current-row
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column align="center" prop="type" label="日志类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.type === 1" type="danger">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="日志标题" />
      <el-table-column prop="mapping" label="请求映射" />
      <el-table-column prop="mode" label="请求方式" />
      <el-table-column prop="remoteIp" label="请求地址" />
      <el-table-column prop="remoteRegion" label="请求区域" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(['sys:log:view'])" type="primary" icon="el-icon-view" @click="viewHandler(scope.row)">查看</el-button>
          <el-button v-if="checkPermission(['sys:log:delete'])" type="danger" icon="el-icon-close" @click="deleteHandler(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableTotal>0"
      :total="tableTotal"
      :page.sync="searchModel.pageNum"
      :limit.sync="searchModel.pageSize"
      @pagination="fetchList"
    />
  </el-main>
</template>

<script>
import checkPermission from '@/utils/permission'
import { deleteById, getById, getPage, clear } from '@/api/sysLog'
import { getById as getSysUserById } from '@/api/sysUser'
import Pagination from '@/components/Pagination'
import SysDialog from '@/components/SysDialog'
export default {
  name: 'SysLog',
  components: {
    Pagination,
    SysDialog
  },
  props: {},
  data() {
    return {
      loading: false,
      tableList: [],
      tableTotal: 0,
      sysDialog: {
        title: '系统提示',
        type: 'add',
        visible: false,
        width: 620,
        height: 350
      },
      entityModel: {
        id: null,
        type: null,
        title: null,
        mapping: null,
        mode: null,
        method: null,
        params: null,
        result: null,
        exception: null,
        startTime: null,
        endTime: null,
        remoteIp: null,
        remoteRegion: null,
        createBy: null,
        createTime: null
      },
      searchModel: {
        title: null,
        pageNum: 1,
        pageSize: 10
      },
      searchRules: {
        title: [{ required: true, trigger: 'blur', message: '请输入日志标题' }]
      }
    }
  },
  created() {
    this.fetchList()
  },
  mounted() {},
  methods: {
    checkPermission,
    fetchList(data) {
      this.loading = true
      // 分页信息改变
      if (data !== undefined) {
        this.searchModel.pageNum = data.page
        this.searchModel.pageSize = data.limit
      }
      // 分页获取列表
      getPage(this.searchModel).then(response => {
        if (response.code === 200) {
          this.tableList = response.data.list
          this.tableTotal = response.data.total
          this.searchModel.pageNum = response.data.pageNum
          this.searchModel.pageSize = response.data.pageSize
          this.loading = false
        }
      })
    },
    sysDialogOpen(title, type) {
      // 设置窗口属性
      this.sysDialog.title = title
      this.sysDialog.type = type
      this.sysDialog.visible = true
      // 重置表单内容
      this.$nextTick(() => {
        this.$refs.entityForm.resetFields()
        this.entityModel.id = null
      })
    },
    sysDialogClose() {
      this.sysDialog.visible = false
    },
    sysDialogConfirm() {
      this.$refs.entityForm.validate(valid => {
        if (valid) {
          if (this.sysDialog.type === 'view') {
            this.sysDialogClose()
          }
        }
      })
    },
    searchHandler() {
      this.$refs.searchForm.validate(valid => {
        if (valid) {
          this.fetchList()
        }
      })
    },
    resethHandler() {
      this.$refs.searchForm.resetFields()
      this.searchModel.pageNum = 1
      this.fetchList()
    },
    clearHandler() {
      clear().then((response) => {
        if (response.code === 200) {
          this.$notify.success({ title: '系统提示', message: response.msg })
          this.fetchList()
        }
      })
    },
    viewHandler(row) {
      getById({ id: row.id }).then(response => {
        if (response.code === 200) {
          this.entityModel = response.data
          getSysUserById({ 'id': this.entityModel.createBy }).then((response) => {
            if (response.code === 200) {
              this.entityModel.createBy = response.data.username
            }
            this.sysDialogOpen('查看系统日志', 'view')
          })
        }
      })
    },
    deleteHandler(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.$notify.success({ title: '系统提示', message: response.msg })
            this.fetchList()
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  margin: 0;
  padding: 0;
}
.show-box {
  width: 575px;
  padding: 10px;
  border: 1px solid #f0f0f0;
}
</style>
